package com.example.drainage.ui.calendar

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.drainage.databinding.ItemSchedulingCalendarBinding
import java.util.*

/**
 * @author 创建人 ：yanghaozhang
 * @version 1.0
 * @package 包名 ：com.example.drainage.ui.calendar
 * @createTime 创建时间 ：2021/11/25
 * @modifyBy 修改人 ：
 * @modifyTime 修改时间 ：
 * @modifyMemo 修改备注：
 */
class CalendarAdapter : RecyclerView.Adapter<CalendarAdapter.CalendarViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CalendarViewHolder {
        val binding = ItemSchedulingCalendarBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return CalendarViewHolder(binding)
    }

    override fun onBindViewHolder(holder: CalendarViewHolder, position: Int) {
        val curCalendar = SchedulingCalendarUtil.getCurMonthCalendar(position)
        holder.binding.tvMonth.text = (curCalendar.get(Calendar.MONTH) + 1).toString()
        holder.binding.recyclerViewCalendar.apply {
            if (adapter == null) {
                val calendarMonthAdapter = CalendarMonthAdapter()
                calendarMonthAdapter.selectListener = {
                    this@CalendarAdapter.notifyDataSetChanged()
                }
                adapter = calendarMonthAdapter
            }
            (adapter as CalendarMonthAdapter).apply {
                setNewInstance(SchedulingCalendarUtil.getDayListFromMonthIndex(position))
            }
        }
    }

    override fun getItemCount(): Int {
        return Int.MAX_VALUE
    }

    class CalendarViewHolder(val binding: ItemSchedulingCalendarBinding) : RecyclerView.ViewHolder(binding.root)
}